package com.iflytek.jobs.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.iflytek.jobs.admin.vo.StudentExcel;
import com.iflytek.jobs.service.domain.StuInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.iflytek.jobs.service.service.IStuInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息Controller
 * 
 * @author chenhs
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/jobs/stu")
public class StuInfoController extends BaseController
{
    @Autowired
    private IStuInfoService stuInfoService;

    /**
     * 查询学生信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StuInfo stuInfo)
    {
        startPage();
        List<StuInfo> list = stuInfoService.selectStuInfoList(stuInfo);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("@ss.notRole('demo')")
    public void export(HttpServletResponse response, StuInfo stuInfo)
    {
        List<StuInfo> list = stuInfoService.selectStuInfoList(stuInfo);
        ExcelUtil<StuInfo> util = new ExcelUtil<StuInfo>(StuInfo.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @PreAuthorize("@ss.notRole('demo')")
    public void importData(MultipartFile file) throws Exception {
        ExcelUtil<StudentExcel> util = new ExcelUtil<>(StudentExcel.class);
        List<StudentExcel> studentExcels = util.importExcel(file.getInputStream());
        studentExcels.forEach(stuExcel ->{
            StuInfo stuInfo = BeanUtil.copyProperties(stuExcel, StuInfo.class,"stuClass");
            SysDept dept = new SysDept();
            dept.setDeptName(stuExcel.getStuClass());
            stuInfo.setDept(dept);
            stuInfoService.insertStuInfo(stuInfo);
        });
    }

    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.notRole('demo')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<StudentExcel> util = new ExcelUtil<>(StudentExcel.class);
        util.importTemplateExcel(response, "学生信息模板");
    }

    /**
     * 获取学生信息详细信息
     */
    @GetMapping(value = "/{stuId}")
    public AjaxResult getInfo(@PathVariable("stuId") String stuId)
    {
        return success(stuInfoService.selectStuInfoByStuId(stuId));
    }

    /**
     * 新增学生信息
     */
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult add(@RequestBody StuInfo stuInfo)
    {
        return toAjax(stuInfoService.insertStuInfo(stuInfo));
    }

    /**
     * 修改学生信息
     */
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult edit(@RequestBody StuInfo stuInfo)
    {
        return toAjax(stuInfoService.updateStuInfo(stuInfo));
    }

    /**
     * 删除学生信息
     */
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stuId}")
    @PreAuthorize("@ss.notRole('demo')")
    public AjaxResult remove(@PathVariable String stuId)
    {
        return toAjax(stuInfoService.deleteStuInfoByStuId(stuId));
    }
}
